package cn.tedu.csmall.product.controller;


import cn.tedu.csmall.product.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.CategoryUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理类别关联相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */

@RestController
@RequestMapping("/categories")
@Slf4j
@Api(tags = "2.类别管理模块")
public class CategoryController {

    @Autowired
    ICategoryService categoryService;

    @ApiOperation("添加类别")
    @PostMapping( "/add-new")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid CategoryAddNewDTO categoryAddNewDTO){
        System.out.println("即将处理【添加类别】请求...");
        System.out.println("categoryAddNewDTO:"+categoryAddNewDTO);
            categoryService.addNew(categoryAddNewDTO);
            log.debug("类别【{}】添加成功！",categoryAddNewDTO.getName());
            return JsonResult.ok();
    }

    @ApiOperation("删除类别")
    @PostMapping("/{id:[0-9]+}/deleteById")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType = "long")
    public JsonResult<Void> deleteById(@PathVariable Long id){
        System.out.println("即将处理【根据id删除类别】请求...");
        categoryService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改类别详情")
    @PostMapping("/{id:[0-9]+}/updateById")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType = "long")
    public JsonResult<Void> updateById(@PathVariable Long id, CategoryUpdateDTO categoryUpdateDTO){
        log.debug("开始处理【修改类别详情】请求，修改id={}",id);
        categoryService.updateById(id,categoryUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("启用类别")
    @PostMapping("/{id:[0-9]+}/setEnable")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType = "long")
    public JsonResult<Void> setEnable(@PathVariable Long id){
        log.debug("即将处理【启用类别】请求，启用类别id={}",id);
        categoryService.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("禁用类别")
    @PostMapping("/{id:[0-9]+}/setDisable")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType = "long")
    public JsonResult<Void> setDisable(@PathVariable Long id){
        log.debug("即将处理【禁用类别】请求，禁用类别id={}",id);
        categoryService.setDisable(id);
        return JsonResult.ok();
    }

    @ApiOperation("显示类别")
    @PostMapping("/{id:[0-9]+}/setDisplay")
    @ApiOperationSupport(order = 330)
    @ApiImplicitParam(name = "id",value = "品牌id",required = true,dataType = "long")
    public JsonResult<Void> setDisplay(@PathVariable Long id){
        log.debug("即将处理【显示类别】请求，显示类别id={}",id);
        categoryService.setDisplay(id);
        return JsonResult.ok();
    }

    @ApiOperation("隐藏类别")
    @PostMapping("/{id:[0-9]+}/setHidden")
    @ApiOperationSupport(order = 340)
    @ApiImplicitParam(name = "id",value = "类别id",required = true,dataType = "long")
    public JsonResult<Void> setHidden(@PathVariable Long id){
        log.debug("即将处理【隐藏类别】请求，隐藏类别id={}",id);
        categoryService.setHidden(id);
        return JsonResult.ok();
    }

    @ApiOperation("根据id获取类别表的详细信息")
    @GetMapping("/{id:[0-9]+}")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id",value = "类别id",required = true,dataType = "long")
    public JsonResult<CategoryStandardVO> getCategoryStandardVO(@PathVariable Long id){
        log.debug("即将处理【根据id获取类别表的详细信息】请求");
        CategoryStandardVO category = categoryService.getCategoryStandardVO(id);
        return JsonResult.ok(category);
    }

    @ApiOperation("根据父级类别id获取类别列表")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "parentId",value = "父级类别id",required = true,dataType = "long")
    @GetMapping("/list-by-parent")
    public JsonResult<List<CategoryListItemVO>> listByParentId(Long parentId){
        log.debug("开始处理【根据父级类别id获取类别列表！】");
        if(parentId==null||parentId<0){
            parentId=0L;
        }
        List<CategoryListItemVO> list = categoryService.list(parentId);
        return JsonResult.ok(list);
    }

}
